package com.seanLab.service;

import com.seanLab.domain.TestDocs;
import com.seanLab.domain.TestUser;
import com.seanLab.domain.UserDocs;
import com.seanLab.dto.TestDocsDto;
import com.seanLab.dto.UserDto;
import com.seanLab.repository.ArticleRepo;
import com.seanLab.repository.TestDocsRepo;
import com.seanLab.repository.TestUserRepo;
import com.seanLab.repository.UserDocsRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by ailly on 18-1-2.
 */
@Service
public class TestService {
    @Autowired
    private TestDocsRepo testDocsRepo;

    @Autowired
    private UserDocsRepo userDocsRepo;

    @Autowired
    private TestUserRepo testUserRepo;

    public void generateTestDocs() {
        int size = 480;
        int cateNum = 6;
        int docPerCate = size / cateNum;
//        List<TestDocs> testDocses = testDocsRepo.findAll();

        int generateStrategy = 1;

        //generate sampleDocs
        List<TestDocs> sampleDocs = new ArrayList<>();

        if (generateStrategy == 1) {
            sampleDocs = testDocsRepo.findAll();
        } else {
            for (int cat = 1; cat <= cateNum; cat++) {
                List<TestDocs> cateDoces = testDocsRepo.findByCateID(cat);

                if (cateDoces.size() < docPerCate) {
                    for (TestDocs bobo : cateDoces) {
                        sampleDocs.add(bobo);
                    }
                } else {
                    Integer[] indices = getRandomIndex(cateDoces.size(), docPerCate);
                    for (int i : indices) {
                        sampleDocs.add(cateDoces.get(i));
                    }
                }
            }
        }

        //generate userDocs
        List<UserDocs> userDocs = new ArrayList<>();
        //read testID
        List<String> testIDs = new ArrayList<>();
        List<TestUser> testUsers = testUserRepo.findAll();
        for (TestUser user : testUsers) {
            testIDs.add(user.getTestID());
        }
        int userNum = testIDs.size();
        int catePerUser =  cateNum / userNum;

        int count = 0;
        int u = 0;
        String tmpTestID = testIDs.get(u);
        //平均分配
        int docPerUser = sampleDocs.size() / userNum;

        for (int q = 0; q < sampleDocs.size(); q++) {
            TestDocs sampleDoc = sampleDocs.get(q);
            UserDocs userDoc = sampleDoc.toUserDocs(sampleDoc);
            if (count == docPerUser && u < userNum-1) {
                u++;
                count = 0;
            }
            userDoc.setTestID(testIDs.get(u));
            count++;

            userDocs.add(userDoc);
        }

        //按cate分配
//        for (TestDocs sampleDoc : sampleDocs) {
//            UserDocs userDoc = sampleDoc.toUserDocs(sampleDoc);
//            //allocate testUsers
//            if (count < docPerCate * catePerUser) {
//                userDoc.setTestID(tmpTestID);
//                count++;
//            } else {
//                tmpTestID = testIDs.get(++u);
//                userDoc.setTestID(tmpTestID);
//                count = 1;
//            }
//
//            userDocs.add(userDoc);
//        }
        userDocsRepo.save(userDocs);

    }

    public TestDocsDto getNextDoc(String testID) {
        TestDocsDto testDocsDto = null;
        UserDocs u = userDocsRepo.findByTestIDAndTestFlag(testID, 0);

        if (u != null) {
            testDocsDto = new TestDocsDto();
            UserDocs tmpDoc = u;
            testDocsDto.setContent(tmpDoc.getContent());
            testDocsDto.setUrl(tmpDoc.getUrl());
            testDocsDto.setCateID(tmpDoc.getDocCategory());
            testDocsDto.setTitle(tmpDoc.getTitle());
            tmpDoc.setTestFlag(1);
            userDocsRepo.save(tmpDoc);
        }
        return testDocsDto;
    }

    private Integer[] getRandomIndex(int length, int size) {

        Random rand = new Random();
        HashSet<Integer> index = new HashSet<>();

        while (index.size() < size) {
            index.add(rand.nextInt(length));
        }

        Integer[] indices = index.toArray(new Integer[index.size()]);

        return indices;

    }

    public static void main(String[] args) {
        TestService t = new TestService();
        Integer[] test = t.getRandomIndex(10000, 5);
        for ( int x : test) {
            System.out.println(x);
        }
    }
}
